package com.runen.infinitemovement.widget;


import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

public class XImageView extends AppCompatImageView {
    float arg = .0f;

    public XImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        float ft = arg == 0f ? 0.75f : arg;
        setMeasuredDimension(widthSize, (int) (widthSize * ft));

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        CharSequence charSequence = getContentDescription();
        if (!TextUtils.isEmpty(charSequence)) {
            try {
                arg = Float.parseFloat(charSequence.toString());
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        if (arg == 0f && bm != null) {
            arg = 1f * bm.getHeight() / bm.getWidth();
        }
        super.setImageBitmap(bm);
    }

}